{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 5 - The Small Scale: Nodes and Centrality#"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Configure plotting in Jupyter\n",
    "from matplotlib import pyplot as plt\n",
    "%matplotlib inline\n",
    "plt.rcParams.update({\n",
    "    'figure.figsize': (7.5, 7.5),\n",
    "    'axes.spines.right': False,\n",
    "    'axes.spines.left': False,\n",
    "    'axes.spines.top': False,\n",
    "    'axes.spines.bottom': False})\n",
    "# Seed random number generator\n",
    "from numpy import random as nprand\n",
    "seed = hash(\"Network Science in Python\") % 2**32\n",
    "nprand.seed(seed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import networkx as nx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create empty affiliation network and list of people\n",
    "B = nx.Graph()\n",
    "people = set()\n",
    "# Load data file into network\n",
    "from pathlib import Path\n",
    "data_dir = Path('.') / 'data'\n",
    "with open(data_dir / 'crossley2012' / '50_ALL_2M.csv') as f:\n",
    "    # Parse header\n",
    "    events = next(f).strip().split(\",\")[1:]\n",
    "    # Parse rows\n",
    "    for row in f:\n",
    "        parts = row.strip().split(\",\")\n",
    "        person = parts[0]\n",
    "        people.add(person)\n",
    "        for j, value in enumerate(parts[1:]):\n",
    "            if value != \"0\":\n",
    "                B.add_edge(person, events[j], weight=int(value))\n",
    "# Project into person-person co-affilation network\n",
    "from networkx import bipartite\n",
    "G = bipartite.projected_graph(B, people)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Maud Joachim', 52896.53324605231),\n",
       " ('Ada Wright', 26344.263264276862),\n",
       " ('Patricia Woodlock', 24774.923422322456),\n",
       " ('Emily Duval', 19517.906214119495),\n",
       " ('Mary Leigh', 19404.225833772092),\n",
       " ('Mabel Capper', 18221.36281158177),\n",
       " ('Sylvia Pankhurst', 18127.59688636897),\n",
       " ('Elsie Evans', 15674.80629870346),\n",
       " ('Winifred Mayo', 15600.989680321345),\n",
       " ('Vera Wentworth', 13233.504078942527)]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "betweenness = nx.betweenness_centrality(G, normalized=False)\n",
    "sorted(betweenness.items(), key=lambda x:x[1], reverse=True)[0:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Maud Joachim', 0.11587964174472955),\n",
       " ('Caroline A Downing', 0.11437066100686182),\n",
       " ('Kitty Marion', 0.1134499601244862),\n",
       " ('Mabel Capper', 0.10991776240126276),\n",
       " ('Annie Bell', 0.1083470522111029),\n",
       " ('Grace Chappelow', 0.10818185244249953),\n",
       " ('Winifred Mayo', 0.1080383196581034),\n",
       " ('Ellen Pitfield', 0.10518714292397992),\n",
       " ('Dorothy Agnes Bowker', 0.10493919222545875),\n",
       " ('Mrs Maud Fussell', 0.10490326319130658)]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eigenvector = nx.eigenvector_centrality(G)\n",
    "sorted(eigenvector.items(), key=lambda x:x[1], reverse=True)[0:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Maud Joachim', 0.5357241748956739),\n",
       " ('Caroline A Downing', 0.5009438937877011),\n",
       " ('Winifred Mayo', 0.5009438937877011),\n",
       " ('Mabel Capper', 0.5006919099377073),\n",
       " ('Kitty Marion', 0.49793672684150186),\n",
       " ('Ada Wright', 0.4898501559823633),\n",
       " ('Patricia Woodlock', 0.4886477746471095),\n",
       " ('Vera Wentworth', 0.48769011119851163),\n",
       " ('Evelyn Whurry', 0.4874512815652116),\n",
       " ('Annie Bell', 0.4869743233640714)]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "closeness = nx.closeness_centrality(G)\n",
    "sorted(closeness.items(), key=lambda x:x[1], reverse=True)[0:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Maud Joachim', 19687),\n",
       " ('Caroline A Downing', 18201),\n",
       " ('Kitty Marion', 17696),\n",
       " ('Mabel Capper', 16811),\n",
       " ('Winifred Mayo', 16455),\n",
       " ('Annie Bell', 16065),\n",
       " ('Grace Chappelow', 16018),\n",
       " ('Ellen Pitfield', 14910),\n",
       " ('Mrs Maud Fussell', 14841),\n",
       " ('Dorothy Agnes Bowker', 14750)]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "triangles = nx.triangles(G)\n",
    "sorted(triangles.items(), key=lambda x:x[1], reverse=True)[0:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Maud Joachim', 0.23595330552759),\n",
       " ('Caroline A Downing', 0.34999903851700864),\n",
       " ('Kitty Marion', 0.3670988486671507),\n",
       " ('Mabel Capper', 0.33992518451117176),\n",
       " ('Annie Bell', 0.4233201581027668),\n",
       " ('Grace Chappelow', 0.43461037551551984),\n",
       " ('Winifred Mayo', 0.3480477177545582),\n",
       " ('Ellen Pitfield', 0.4828993392926545),\n",
       " ('Dorothy Agnes Bowker', 0.5058125578683859),\n",
       " ('Mrs Maud Fussell', 0.5006071645415908)]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clustering = nx.clustering(G)\n",
    "[(x, clustering[x]) for x in sorted(people, key=lambda x:eigenvector[x], reverse=True)[0:10]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
